﻿<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <!--[if lt IE 9]>
    <script type="text/javascript" src="lib/html5shiv.js"></script>
    <script type="text/javascript" src="lib/respond.min.js"></script>
    <![endif]-->
    <link href="static/h-ui/css/H-ui.min.css" rel="stylesheet" type="text/css"/>
    <link href="static/h-ui.admin/css/H-ui.login.css" rel="stylesheet" type="text/css"/>
    <link href="static/h-ui.admin/css/style.css" rel="stylesheet" type="text/css"/>
    <link href="lib/Hui-iconfont/1.0.8/iconfont.css" rel="stylesheet" type="text/css"/>
    <!--[if IE 6]>
    <script type="text/javascript" src="lib/DD_belatedPNG_0.0.8a-min.js"></script>
    <script>DD_belatedPNG.fix('*');</script>
    <![endif]-->
    <title>后台登录 - H-ui.admin v3.0</title>
    <meta name="keywords" content="H-ui.admin v3.0,H-ui网站后台模版,后台模版下载,后台管理系统模版,HTML后台模版下载">
    <meta name="description"
          content="H-ui.admin v3.0，是一款由国人开发的轻量级扁平化网站后台模板，完全免费开源的网站后台管理系统模版，适合中小型CMS后台系统。">
</head>
<body>
<input type="hidden" id="TenantId" name="TenantId" value=""/>
<div class="header"></div>
<div id="app" class="loginWraper">
    <div id="loginform" class="loginBox">
        <h5 style="text-align: center;margin: 0px auto;padding: 0px;color:cornflowerblue;font-weight: bolder;">
            用户注册</h5>
        <div class="form form-horizontal">
            <div class="row cl">
                <label class="form-label col-xs-3"><i class="Hui-iconfont">&#xe60d;</i></label>
                <div class="formControls col-xs-8">
                    <input id="identify" name="identify" type="text" placeholder="请输入注册手机号码/电子邮箱"
                           class="input-text size-L" v-model="regUser.identify">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-3"><i class="Hui-iconfont">&#xe60e;</i></label>
                <div class="formControls col-xs-8">
                    <input id="password" name="password" type="password" placeholder="请输入密码"
                           class="input-text size-L" v-model="regUser.password">
                    <br>
                    <input id="confirmpass" name="password" type="password" placeholder="请输入确认密码"
                           class="input-text size-L" v-model="regUser.confirmPass">
                </div>
            </div>
            <div class="row cl">
                <div class="formControls col-xs-8 col-xs-offset-3">
                    <input class="input-text size-L" type="text" id="randomCode" placeholder="请输入验证码"
                           value="" style="width:120px;" v-model="validateCode">
                    <input name="" type="reset" class="btn btn-default radius size-L"
                           :value="msgButtonTitle" :disabled="msgButtonEnabled" @click="sendMsg">
                </div>
            </div>

            <div class="row cl">
                <div class="formControls col-xs-8 col-xs-offset-3">
                    <!--
                    <input name="" type="submit" class="btn btn-primary radius size-L"
                           value="&nbsp;注&nbsp;&nbsp;&nbsp;&nbsp;册&nbsp;">
                    -->
                    <input name="" type="button" class="btn btn-primary radius size-L"
                           value="注  册" @click="doReg">

                    <input name="" type="reset" class="btn btn-default radius size-L"
                           value="取  消">
                    &nbsp;&nbsp;<a href="login.html">立即登录</a>
                </div>
            </div>

        </div>
    </div>
</div>
<div class="footer"> &copy; 华清远见 2017-2023 &nbsp;&nbsp; 作者: 西蒙牛 v1.0</div>
<script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="static/h-ui/js/H-ui.min.js"></script>

<!--引入基础的公共类库-->
<script src="include/commons.js"></script>
<script src="auth/auth.js"></script>
<script src="uitl/outils.min.js"></script>
<script src="uitl/encrypt.js"></script>


<script type="module">
    if (await checkLogin()) {
        console.log("已登录....");
        window.location.href = 'index.html';
    } else {
        console.log('未登录....')
    }
</script>

<!--此乃百度统计代码，请自行删除-->
<script type="module">
    /*
    var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?080836300300be57b7f34f4b3e97d911";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();*/
    //引入前端nanoid令牌生成工具类库
    import {nanoid} from "./uitl/nanoid.js";

    //为了实现防抖，创建一个定时器对象
    let timer ;
    let vm = new Vue({
        el: "#app",
        data: {
            token: nanoid(16),
            validateCode: '',
            regUser: {
                identify: '',
                password: '',
                confirmPass: ''
            },
            msgButtonEnabled: false,
            msgButtonTitle: '发送验证码'
        },
        mounted() {

        },
        methods: {

            //给手机或者邮箱发送验证码的方法
            async sendMsg() {
                console.log('执行发送验证码....');
                if (!outils.isPhoneNum(this.regUser.identify) && !outils.isEmail(this.regUser.identify)) {
                    console.log('注册信息格式非法....');
                    this.$message({
                        message: '注册信息格式非法!',
                        type: 'error'
                    });
                    return;
                }

                //再检查该手机或者邮箱是否已经注册
                /*
                let result = await checkIdentifyIsReg(this.regUser.identify).then(resp => {
                    return resp.data.code;
                })

                if (result === 400) {
                    this.$message({
                        message: '该手机或者邮箱已经被注册！',
                        type: 'error'
                    });
                    return; //啥也不做了。。。。。
                }*/

                let params = {
                    identify: this.regUser.identify,
                    token: this.token
                };
                console.log('---------发送短信验证码时要传递的参数---------');
                console.log(params);

                //这里准备发送验证码，实现防抖功能；
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(() => {
                    sendRadomCodeMsg(params).then(resp => {
                        //console.log(resp);
                        if (resp.data.code === 200) {
                            this.$message({
                                message: '验证码发送成功!',
                                type: 'success'
                            });
                            let counter = 60;
                            this.msgButtonEnabled = true; //发送验证码的按钮不可用。
                            //'重新发送验证码(60)'
                            this.msgButtonTitle = "重新发送验证码(" + counter + ")"
                            //倒计时60秒之后，再可用
                            let intervalTimer = setInterval(() => {
                                counter--;
                                if (counter === 0) {
                                    clearInterval(intervalTimer); //清除定时器；
                                    this.msgButtonEnabled = false;
                                    this.msgButtonTitle = '发送验证码';
                                } else {
                                    this.msgButtonTitle = "重新发送验证码(" + counter + ")"
                                }
                            }, 1000)

                        } else {
                            this.$message({
                                message: '验证码发送失败!',
                                type: 'error'
                            });
                        }
                    })

                }, 2000)

            },

            //注册时的表单验证
            regFormValidate() {

                if (!outils.isPhoneNum(this.regUser.identify) && !outils.isEmail(this.regUser.identify)) {
                    console.log('注册信息格式非法....');
                    this.$message({
                        message: '注册信息格式非法!',
                        type: 'error'
                    });
                    return false;
                }
                if (this.regUser.password.length < 6) {
                    console.log('密码长度小于6位....');
                    this.$message({
                        message: '密码长度小于6位!',
                        type: 'error'
                    });
                    return false;
                }

                if (this.regUser.password !== this.regUser.confirmPass) {
                    console.log('密码与确认密码不一致！');
                    this.$message({
                        message: '密码与确认密码不一致！',
                        type: 'error'
                    });
                    return false;
                }
                return true;
            },


            //检查验证码是否正确
            async checkRandomCode() {
                let params = {
                    token: this.token,
                    code: this.validateCode
                }

                return await validateRandomCode(params).then(resp => {
                    console.log("检查验证码是否正确返回的状态码是：" + resp.data.code)
                    return resp.data.code === 200;
                });
            },

            async doReg() {
                //表单验证
                if (!this.regFormValidate()) {
                    console.log('表单验证不通过....')
                    return;
                }

                //检查验证码是否正确
                if (!await this.checkRandomCode()) {
                    this.$message({
                        message: '验证验证失败！',
                        type: 'error'
                    });
                    return;
                }

                if (outils.isPhoneNum(this.regUser.identify)) {
                    this.regUser.mobile = this.regUser.identify;
                }
                if (outils.isEmail(this.regUser.identify)) {
                    this.regUser.email = this.regUser.identify;
                }

                console.log(this.regUser);

                reg(this.regUser).then(resp => {
                    //console.log(resp);
                    if (resp.data.code === 200) {
                        this.$message({
                            message: resp.data.msg,
                            type: 'success'
                        });
                        //起一个定时器，一秒钟之后跳转到商城首页
                        setTimeout(() => {
                            window.location.href = 'login.html'
                        }, 1000);
                    }
                })
            }
        }
    })
</script>
<!--/此乃百度统计代码，请自行删除
</body>
</html>